﻿using Coolzon.Common;
using Coolzon.Web.Models;
using Coolzon.WebServer.Code.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Odbc;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web;
using System.Web.Http;
using RT = Coolzon.Metadata.Runtime;

namespace Coolzon.WebServer.Code.ApiControllers
{
    public class DataController : ApiController
    {
        private static List<int> s_rtdata1 = new List<int>();
        private static List<int> s_rtdata2 = new List<int>();

        [HttpGet]
        public string TestLoad()
        {
            DataResult result = new DataResult();
            string strconn = "DSN=pro-10.0.1.240";
            OdbcConnection connection = null;
            try
            {
                connection = new OdbcConnection(strconn);
                OdbcDataAdapter da = new OdbcDataAdapter(@"SELECT egv.MC_OBJECT as 'object',egv.MC_OBJECT_CLASS as 'type',
egv.SEVERITY as 'level',egv.MC_ARRIVAL_TIME as 'time',
egv.MSG as 'content' FROM EG_EVENT_VIEW egv where egv.STATUS = 'OPEN' AND egv.SEVERITY in ('MAJOR','CRITICAL') order by 'time' desc, 'level'", connection);
                DataTable dataTable = new DataTable();

                da.Fill(dataTable);

                result.DataSeries = new List<DataSeries>();
                for (int seriesIndex = 0; seriesIndex < dataTable.Rows.Count; seriesIndex++)
                {
                    DataSeries series = new DataSeries();
                    series.Id = string.Format("series{0}", seriesIndex);
                    series.Name = series.Name;
                    series.DataItems = new List<DataItem>();
                    result.DataSeries.Add(series);

                    DataItem itemObject = new DataItem();
                    itemObject.Id = "object";
                    itemObject.Name = itemObject.Id;
                    itemObject.Value = Convert.ToString(dataTable.Rows[seriesIndex]["object"]);
                    series.DataItems.Add(itemObject);

                    DataItem itemType = new DataItem();
                    itemType.Id = "type";
                    itemType.Name = itemType.Id;
                    itemType.Value = Convert.ToString(dataTable.Rows[seriesIndex]["type"]);
                    series.DataItems.Add(itemType);

                    string strLevel = Convert.ToString(dataTable.Rows[seriesIndex]["level"]); ;
                    DataItem itemLevel = new DataItem();
                    itemLevel.Id = "level";
                    itemLevel.Name = itemLevel.Id;
                    if (string.Equals(strLevel, "CRITICAL"))
                    {
                        itemLevel.Value = "3";
                    }
                    else
                    {
                        itemLevel.Value = "0";
                    }
                    series.DataItems.Add(itemLevel);

                    DataItem itemTime = new DataItem();
                    itemTime.Id = "time";
                    itemTime.Name = itemTime.Id;
                    itemTime.Value = new DateTime(Convert.ToInt64(dataTable.Rows[seriesIndex]["time"])).ToString("yyyy/MM/dd HH:mm:ss");
                    series.DataItems.Add(itemTime);

                    DataItem itemContent = new DataItem();
                    itemContent.Id = "content";
                    itemContent.Name = itemContent.Id;
                    itemContent.Value = Convert.ToString(dataTable.Rows[seriesIndex]["content"]);
                    series.DataItems.Add(itemContent);
                }
            }
            catch (Exception ex)
            {
                Logger.ErrorToTag("Sybase", string.Format("获取数据Pro-10.0.1.240,event-list出错,ex:{0}", ex));
                return Environment.Is64BitProcess.ToString();
            }
            finally
            {
                if (connection != null)
                    connection.Close();
            }
            return "ok";
        }

        [HttpPost]
        public ServiceResult<DataResult> Load(LoadDataParam param)
        {
            DataResult result = new DataResult();
            if (string.Equals(param.DataSource, "Pro-10.0.1.240"))
            {
                if (string.Equals(param.DataSet, "event-list"))
                {
                    string strconn = "DSN=pro-10.0.1.240";
                    OdbcConnection connection = new OdbcConnection(strconn);
                    OdbcDataAdapter da = new OdbcDataAdapter(@"SELECT egv.MC_OBJECT as 'object',egv.MC_OBJECT_CLASS as 'type',
egv.SEVERITY as 'level',egv.MC_ARRIVAL_TIME as 'time',
egv.MSG as 'content' FROM EG_EVENT_VIEW egv where egv.STATUS = 'OPEN' AND egv.SEVERITY in ('MAJOR','CRITICAL') order by 'time' desc, 'level'", connection);
                    DataTable dataTable = new DataTable();

                    try
                    {
                        da.Fill(dataTable);

                    }
                    catch (Exception ex)
                    {
                        Logger.ErrorToTag("Sybase", string.Format("获取数据Pro-10.0.1.240,event-list出错,ex:{0}", ex));
                        return null;
                    }
                    finally
                    {
                        connection.Close();
                    }
                    result.DataSeries = new List<DataSeries>();
                    for (int seriesIndex = 0; seriesIndex < dataTable.Rows.Count; seriesIndex++)
                    {
                        DataSeries series = new DataSeries();
                        series.Id = string.Format("series{0}", seriesIndex);
                        series.Name = series.Name;
                        series.DataItems = new List<DataItem>();
                        result.DataSeries.Add(series);

                        DataItem itemObject = new DataItem();
                        itemObject.Id = "object";
                        itemObject.Name = itemObject.Id;
                        itemObject.Value = Convert.ToString(dataTable.Rows[seriesIndex]["object"]);
                        series.DataItems.Add(itemObject);

                        DataItem itemType = new DataItem();
                        itemType.Id = "type";
                        itemType.Name = itemType.Id;
                        itemType.Value = Convert.ToString(dataTable.Rows[seriesIndex]["type"]);
                        series.DataItems.Add(itemType);

                        string strLevel = Convert.ToString(dataTable.Rows[seriesIndex]["level"]); ;
                        DataItem itemLevel = new DataItem();
                        itemLevel.Id = "level";
                        itemLevel.Name = itemLevel.Id;
                        if (string.Equals(strLevel, "CRITICAL"))
                        {
                            itemLevel.Value = "3";
                        }
                        else
                        {
                            itemLevel.Value = "0";
                        }
                        series.DataItems.Add(itemLevel);

                        DataItem itemTime = new DataItem();
                        itemTime.Id = "time";
                        itemTime.Name = itemTime.Id;
                        //计算机归定的日期起始时间
                        DateTime dateTime = new DateTime(1970,1,1).AddHours(8);//1970-1-1 08:00:00
                        //加上秒数
                        dateTime = dateTime.AddSeconds(Convert.ToInt64(dataTable.Rows[seriesIndex]["time"]));
                        itemTime.Value = dateTime.ToString("yyyy/MM/dd HH:mm:ss");
                        series.DataItems.Add(itemTime);

                        DataItem itemContent = new DataItem();
                        itemContent.Id = "content";
                        itemContent.Name = itemContent.Id;
                        itemContent.Value = Convert.ToString(dataTable.Rows[seriesIndex]["content"]);
                        series.DataItems.Add(itemContent);
                    }
                }
            }
            else if (string.Equals(param.DataSource, "MOCKDATA"))
            {
                if (string.Equals(param.DataSet, "Table1"))
                {
                    #region table data
                    result.DataSeries = new List<DataSeries>();
                    for (int seriesIndex = 0; seriesIndex < 30; seriesIndex++)
                    {
                        DataSeries series = new DataSeries();
                        series.Id = string.Format("series{0}", seriesIndex);
                        series.Name = series.Name;
                        series.DataItems = new List<DataItem>();
                        result.DataSeries.Add(series);

                        for (int index = 0; index < 3; index++)
                        {
                            DataItem item = new DataItem();
                            item.Id = string.Format("text{0}", index);
                            item.Name = item.Id;
                            item.Value = string.Format("第{0}行文本内容-{1}", seriesIndex, index);
                            series.DataItems.Add(item);
                        }

                        for (int index = 0; index < 3; index++)
                        {
                            DataItem item = new DataItem();
                            item.Id = string.Format("time{0}", index);
                            item.Name = item.Id;
                            item.Value = DateTime.Now.AddDays(-30).AddDays(new Random().Next(0, 10)).AddHours(new Random().Next(0, 48)).AddMinutes(new Random().Next(0, 100)).ToString("yyyy-MM-dd");
                            series.DataItems.Add(item);
                        }

                        for (int index = 0; index < 3; index++)
                        {
                            DataItem item = new DataItem();
                            item.Id = string.Format("bool{0}", index);
                            item.Name = item.Id;
                            if (new Random().Next(0, 3) == 1)
                            {
                                item.Value = "1";
                            }
                            else
                            {
                                item.Value = "0";
                            }
                            series.DataItems.Add(item);
                            System.Threading.Thread.Sleep(20);
                        }
                    }
                    #endregion
                }
                else if (string.Equals(param.DataSet, "RTDATA01"))
                {
                    result.DataSeries = new List<DataSeries>();
                    for (int seriesIndex = 0; seriesIndex < 1; seriesIndex++)
                    {
                        DataSeries series = new DataSeries();
                        series.Id = string.Format("series{0}", seriesIndex);
                        series.Name = series.Name;
                        series.DataItems = new List<DataItem>();
                        result.DataSeries.Add(series);

                        if (s_rtdata1.Count == 0)
                        {
                            for (int itemIndex = 0; itemIndex < 288; itemIndex++)
                            {
                                DataItem item = new DataItem();
                                item.Id = string.Format("item{0}", itemIndex);
                                item.Name = DateTime.Now.AddSeconds(itemIndex).ToString("yyyy-MM-dd HH:mm:ss");
                                int value = new Random().Next(100, 180);
                                System.Threading.Thread.Sleep(20);
                                item.Value = value.ToString();
                                series.DataItems.Add(item);
                                s_rtdata1.Add(value);
                            }
                        }
                        else
                        {
                            s_rtdata1.RemoveAt(0);
                            s_rtdata1.Add(new Random().Next(100, 180));
                            for (int itemIndex = 0; itemIndex < 288; itemIndex++)
                            {
                                DataItem item = new DataItem();
                                item.Id = string.Format("item{0}", itemIndex);
                                item.Name = DateTime.Now.AddSeconds(itemIndex).ToString("yyyy-MM-dd HH:mm:ss");
                                int value = s_rtdata1[itemIndex];
                                item.Value = value.ToString();
                                series.DataItems.Add(item);
                            }
                        }
                    }
                }
                else if (string.Equals(param.DataSet, "RTDATA02"))
                {
                    result.DataSeries = new List<DataSeries>();
                    for (int seriesIndex = 0; seriesIndex < 1; seriesIndex++)
                    {
                        DataSeries series = new DataSeries();
                        series.Id = string.Format("series{0}", seriesIndex);
                        series.Name = series.Name;
                        series.DataItems = new List<DataItem>();
                        result.DataSeries.Add(series);
                        if (s_rtdata2.Count == 0)
                        {
                            for (int itemIndex = 0; itemIndex < 24; itemIndex++)
                            {
                                DataItem item = new DataItem();
                                item.Id = string.Format("item{0}", itemIndex);
                                item.Name = DateTime.Now.AddSeconds(itemIndex).ToString("yyyy-MM-dd HH:mm:ss");
                                int value = new Random().Next(0, 100);
                                System.Threading.Thread.Sleep(20);
                                item.Value = value.ToString();
                                series.DataItems.Add(item);
                                s_rtdata2.Add(value);
                            }
                        }
                        else
                        {
                            s_rtdata2.RemoveAt(0);
                            s_rtdata2.Add(new Random().Next(0, 100));
                            for (int itemIndex = 0; itemIndex < 24; itemIndex++)
                            {
                                DataItem item = new DataItem();
                                item.Id = string.Format("item{0}", itemIndex);
                                item.Name = DateTime.Now.AddSeconds(itemIndex).ToString("yyyy-MM-dd HH:mm:ss");
                                int value = s_rtdata2[itemIndex];
                                item.Value = value.ToString();
                                series.DataItems.Add(item);
                            }
                        }
                    }
                }
                else if (string.Equals(param.DataSet, "Random0-100"))
                {
                    result.DataSeries = new List<DataSeries>();
                    for (int seriesIndex = 0; seriesIndex < 1; seriesIndex++)
                    {
                        DataSeries series = new DataSeries();
                        series.Id = string.Format("series{0}", seriesIndex);
                        series.Name = series.Name;
                        series.DataItems = new List<DataItem>();
                        result.DataSeries.Add(series);
                        for (int itemIndex = 0; itemIndex < 20; itemIndex++)
                        {
                            DataItem item = new DataItem();
                            item.Id = string.Format("item{0}", itemIndex);
                            item.Name = item.Id;
                            item.Value = (new Random().Next(0, 100)).ToString();
                            series.DataItems.Add(item);
                            System.Threading.Thread.Sleep(20);
                        }
                    }
                }
                else if (string.Equals(param.DataSet, "Random0-50"))
                {
                    result.DataSeries = new List<DataSeries>();
                    for (int seriesIndex = 0; seriesIndex < 1; seriesIndex++)
                    {
                        DataSeries series = new DataSeries();
                        series.Id = string.Format("series{0}", seriesIndex);
                        series.Name = series.Name;
                        series.DataItems = new List<DataItem>();
                        result.DataSeries.Add(series);
                        for (int itemIndex = 0; itemIndex < 20; itemIndex++)
                        {
                            DataItem item = new DataItem();
                            item.Id = string.Format("item{0}", itemIndex);
                            item.Name = item.Id;
                            item.Value = (new Random().Next(0, 50)).ToString();
                            series.DataItems.Add(item);
                            System.Threading.Thread.Sleep(20);
                        }
                    }
                }
                else if (string.Equals(param.DataSet, "Random0-10"))
                {
                    result.DataSeries = new List<DataSeries>();
                    for (int seriesIndex = 0; seriesIndex < 1; seriesIndex++)
                    {
                        DataSeries series = new DataSeries();
                        series.Id = string.Format("series{0}", seriesIndex);
                        series.Name = series.Name;
                        series.DataItems = new List<DataItem>();
                        result.DataSeries.Add(series);
                        for (int itemIndex = 0; itemIndex < 20; itemIndex++)
                        {
                            DataItem item = new DataItem();
                            item.Id = string.Format("item{0}", itemIndex);
                            item.Name = item.Id;
                            item.Value = (new Random().Next(0, 10)).ToString();
                            series.DataItems.Add(item);
                            System.Threading.Thread.Sleep(20);
                        }
                    }
                }
            }
            else
            {
                result.DataSeries = new List<DataSeries>();
                for (int seriesIndex = 0; seriesIndex < 5; seriesIndex++)
                {
                    DataSeries series = new DataSeries();
                    series.Id = string.Format("series{0}", seriesIndex);
                    series.Name = series.Name;
                    series.DataItems = new List<DataItem>();
                    result.DataSeries.Add(series);
                    for (int itemIndex = 0; itemIndex < 10; itemIndex++)
                    {
                        DataItem item = new DataItem();
                        item.Id = string.Format("item{0}", itemIndex);
                        item.Name = item.Id;
                        item.Value = ((itemIndex * 10) + new Random().Next(100, 500)).ToString();
                        System.Threading.Thread.Sleep(20);
                        series.DataItems.Add(item);
                    }
                }
            }
            return new ServiceResult<DataResult>(result);
        }
    }
}